package com.example.chasshelper.dao;

import com.example.chasshelper.model.Classroom;
import com.example.chasshelper.model.Course;
import com.example.chasshelper.model.Message;
import com.example.chasshelper.model.Student;
import com.example.chasshelper.model.Teacher;
import com.example.chasshelper.model.Test;
import com.example.chasshelper.utils.OkHttpUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class TestDao {
    private OkHttpClient okHttpClient = OkHttpUtils.getClient();
    private String ipUrl = OkHttpUtils.getUrl();
    private String url;

    /*
     * 查询所有
     * */
    public List<Test> findAll() {
        url = ipUrl + "/test/findAll";
        System.out.println("url:"+url);
        List<Test> list = new ArrayList<>();
        Request request = new Request.Builder().url(url).get().build();
        try {
            //执行同步get请求
            Response response = okHttpClient.newCall(request).execute();
            //获得返回的字符串
            String string =response.body().string();
            //开始解析服务器传过来的json数据
            Gson gson = new Gson();
            //传过来的是list，需要用泛型来接收
            Type type = new TypeToken<List<Test>>() {}.getType();
            list = gson.fromJson(string, type);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }


    /*
     * 根据id查询教师用户
     * */
    public Teacher findTeacher(Test test) {
        url = ipUrl + "/test/findTeacher?testId=" + test.getTestId();
        System.out.println("url:"+url);
        Teacher teacher=null;
        Request request = new Request.Builder().url(url).get().build();
        try {
            //执行同步get请求
            Response response = okHttpClient.newCall(request).execute();
            //获得返回的字符串
            String string =response.body().string();
            //开始解析服务器传过来的json数据
            Gson gson = new Gson();
            //接收数据
            teacher = gson.fromJson(string, Teacher.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(teacher);
        return teacher;
    }

    /*
     * 根据id查询
     * */
    public  Test findById(Integer id) {
        url = ipUrl + "/test/findById?testId=" + id;
        System.out.println("url:"+url);
        Test test=new Test();
        OkHttpClient okHttpClient = OkHttpUtils.getClient();
        Request request = new Request.Builder().url(url).get().build();
        try {
            //执行同步get请求
            Response response = okHttpClient.newCall(request).execute();
            //获得返回的字符串
            String string =response.body().string();
            //开始解析服务器传过来的json数据
            Gson gson = new Gson();
            //接收数据
            test = gson.fromJson(string, Test.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return test;
    }

    /*
     * 根据教师id查询题目
     * */
    public List<Test> findByTeacherId(Teacher teacher) {
        url = ipUrl + "/test/findByTeacherId?teacherId="+teacher.getTeacherId();
        System.out.println("url:"+url);
        List<Test> list = new ArrayList<>();
        Request request = new Request.Builder().url(url).get().build();
        try {
            //执行同步get请求
            Response response = okHttpClient.newCall(request).execute();
            //获得返回的字符串
            String string =response.body().string();
            //开始解析服务器传过来的json数据
            Gson gson = new Gson();
            //传过来的是list，需要用泛型来接收
            Type type = new TypeToken<List<Test>>() {}.getType();
            list = gson.fromJson(string, type);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    /*
     * 根据课程名或id模糊查询
     * */
    public List<Test> findByIdOrTestContextLike(String factor) {
        System.out.println(factor);
        if (factor==null||factor.equals("")) {
            url = ipUrl + "/test/findByIdOrTestContextLike";
        } else {
            url = ipUrl + "/test/findByIdOrTestContextLike?factor=" + factor;
        }

        System.out.println("url:"+url);
        List<Test> list = new ArrayList<>();
        Request request = new Request.Builder().url(url).get().build();
        try {
            //执行同步get请求
            Response response = okHttpClient.newCall(request).execute();
            //获得返回的字符串
            String string =response.body().string();
            //开始解析服务器传过来的json数据
            Gson gson = new Gson();
            //传过来的是list，需要用泛型来接收
            Type type = new TypeToken<List<Test>>() {}.getType();
            list = gson.fromJson(string, type);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    /*
     * 创建题目
     * */
    public void insert(Test test, Teacher teacher) {
        if (Integer.valueOf(test.getTestItemNum())==2) {
            url = ipUrl + "/test/insert?testContext="+test.getTestContext()+"&testType="+test.getTestType()+"&testAnswer="+test.getTestAnswer()+"&testItemNum="+test.getTestItemNum()+"&testItem1="+test.getTestItem1()+"&testItem2="+test.getTestItem2()+"&teacherId="+teacher.getTeacherId();
        }
        if (Integer.valueOf(test.getTestItemNum())==3) {
            url = ipUrl + "/test/insert?testContext="+test.getTestContext()+"&testType="+test.getTestType()+"&testAnswer="+test.getTestAnswer()+"&testItemNum="+test.getTestItemNum()+"&testItem1="+test.getTestItem1()+"&testItem2="+test.getTestItem2()+"&testItem3="+test.getTestItem3()+"&teacherId="+teacher.getTeacherId();
        }
        if (Integer.valueOf(test.getTestItemNum())==4) {
            url = ipUrl + "/test/insert?testContext="+test.getTestContext()+"&testType="+test.getTestType()+"&testAnswer="+test.getTestAnswer()+"&testItemNum="+test.getTestItemNum()+"&testItem1="+test.getTestItem1()+"&testItem2="+test.getTestItem2()+"&testItem3="+test.getTestItem3()+"&testItem4="+test.getTestItem4()+"&teacherId="+teacher.getTeacherId();
        }
        if (Integer.valueOf(test.getTestItemNum())==5) {
            url = ipUrl + "/test/insert?testContext="+test.getTestContext()+"&testType="+test.getTestType()+"&testAnswer="+test.getTestAnswer()+"&testItemNum="+test.getTestItemNum()+"&testItem1="+test.getTestItem1()+"&testItem2="+test.getTestItem2()+"&testItem3="+test.getTestItem3()+"&testItem4="+test.getTestItem4()+"&testItem5="+test.getTestItem5()+"&teacherId="+teacher.getTeacherId();
        }
        System.out.println("url:"+url);
        Request request = new Request.Builder().url(url).get().build();
        final Map<String, Object> resultMap = new HashMap<>();
        //执行异步get请求
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                System.out.println("失败");
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                System.out.println("成功");
            }
        });
    }

}
